/**
 * 
 */
package TP1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;
import java.util.Map.Entry;

public class Dictionnaire {
	protected String repertoire;
	HashMap<String, Double> idf;
	protected int nbDoc;

	Dictionnaire(String repertoire) {
		this.repertoire = repertoire;
		idf = new HashMap<String, Double>();
	}

	public void calculIdf() {
		nbDoc = 0;
		String[] repertoires = null;
		String[] files = null;
		File directoryToScan = new File(repertoire);
		repertoires = directoryToScan.list();

		for (int k = 0; k < repertoires.length; k++) {
			System.out.println("Partie " + k);
			File sousRepertoire = new File(repertoire + "/" + repertoires[k]);
			files = sousRepertoire.list();
			for (int i = 0; i < files.length; i++) {
				// lecture du fichier texte
				nbDoc++;
				try {
					InputStream ips = new FileInputStream(repertoire + "/"
							+ repertoires[k] + "/" + files[i]);
					InputStreamReader ipsr = new InputStreamReader(ips);
					BufferedReader br = new BufferedReader(ipsr);
					String ligne;

					while ((ligne = br.readLine()) != null) {
						String mots[] = ligne.split(" ");
						for (int j = 0; j < mots.length; j++) {
							if (!idf.containsKey(mots[j])) {
								idf.put(mots[j], 1.0);
							} else {
								boolean ajouter = true;
								for (int l = 0; l < j; l++) {
									if (mots[j].equals(mots[l]))
										ajouter = false;
								}
								if (ajouter)
									idf.put(mots[j], idf.get(mots[j]) + 1);
							}
						}
					}
					br.close();
				} catch (Exception e) {
					System.out.println(e.toString());
				}
			}
		}
		// Calcule idf
		double somme = 0;
		for (Entry<String, Double> dico : idf.entrySet()) {
			dico.setValue(Math.log(nbDoc / dico.getValue()));
			somme += dico.getValue();
		}
		// Normalisation
		for (Entry<String, Double> dico : idf.entrySet()) {
			dico.setValue(dico.getValue() / somme);
		}
		// print();
	}

	/**
	 * @param log
	 * @return
	 */

	public void print() {
		if (idf != null) {
			for (Entry<String, Double> hit : idf.entrySet()) {
				System.out.println("mot : " + hit.getKey() + "\t idf : "
						+ hit.getValue().toString());
			}
		}
	}

	public HashMap<String, Double> tfIdfDocument(String document) {
		HashMap<String, Double> tfIdf = new HashMap<String, Double>();

		try {
			InputStream ips = new FileInputStream(repertoire + "/" + document);
			InputStreamReader ipsr = new InputStreamReader(ips);
			BufferedReader br = new BufferedReader(ipsr);
			String ligne;

			while ((ligne = br.readLine()) != null) {
				String mots[] = ligne.split(" ");
				for (int j = 0; j < mots.length; j++) {
					if (!tfIdf.containsKey(mots[j])) {
						tfIdf.put(mots[j], calculTfIdf(mots, mots[j]));
					}
				}
			}
			br.close();
		} catch (Exception e) {
			System.out.println(e.toString());
		}

		return tfIdf;
	}

	public Double calculTfIdf(String[] document, String mot) {
		int occDoc = 0;
		int nbMotDoc = 0;
		try {
			for (int j = 0; j < document.length; j++) {
				if (document[j].equals(mot)) {
					occDoc++;
				}
				nbMotDoc++;
			}
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		double tf = (double) occDoc / (double) nbMotDoc;

		return tf * idf.get(mot);
	}
}
